﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using BUS;
using DAO;

namespace ProjectM2Work
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            this.ItemsListTu = new ObservableCollection<ItemTuVungModel>();
        }

        /// <summary>
        /// danh sách từ của từ điẻn
        /// </summary>
        public ObservableCollection<ItemTuVungModel> ItemsListTu
        { get; private set; }

        private string _sampleProperty = "Sample Runtime Property Value";
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding
        /// </summary>
        /// <returns></returns>
        private string SampleProperty
        {
            get
            {
                return _sampleProperty;
            }
            set
            {
                if (value != _sampleProperty)
                {
                    _sampleProperty = value;
                    NotifyPropertyChanged("SampleProperty");
                }
            }
        }

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public void LoadData()
        {
            this.IsDataLoaded = true;
        }

        /// <summary>
        /// fill data in listTuVung
        /// </summary>
        public void LoadDataSelected()
        {
            TuVungBUS tuVungBUS = new TuVungBUS();
            List<TuDien> lstTuDien = tuVungBUS.LayDanhSachTuVung();

            this.ItemsListTu.Clear();

            for (int i = 0; i < lstTuDien.Count; i++)
            {
                ItemTuVungModel temp = new ItemTuVungModel();
                temp.LineOne = lstTuDien[i].VnWord;
                temp.LineTwo = tuVungBUS.LayVungTuDien(lstTuDien[i]);
                temp.LineThree = lstTuDien[i].ID;
                ItemsListTu.Add(temp);
            }
        }

        public void LoadTuDienTimKiem(string keyword)
        {
            TuVungBUS tuVungBUS = new TuVungBUS();
            List<TuDien> lstTuDien;
            this.ItemsListTu.Clear();
            if (keyword == "")
                lstTuDien = tuVungBUS.LayDanhSachTuVung();
            else
                lstTuDien = tuVungBUS.TimKiemTuDien(keyword);
            
            for (int i = 0; i < lstTuDien.Count; i++)
            {
                ItemTuVungModel temp = new ItemTuVungModel();
                temp.LineOne = lstTuDien[i].VnWord;
                temp.LineTwo = tuVungBUS.LayVungTuDien(lstTuDien[i]);
                temp.LineThree = lstTuDien[i].ID;
                ItemsListTu.Add(temp);
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}